package xyz.cerberu.cet.search.server.impl;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.cerberu.cet.anime.domain.Anime;
import xyz.cerberu.cet.global.domain.Global;
import xyz.cerberu.cet.resource.domian.Resource;
import xyz.cerberu.cet.search.server.SearchService;
import xyz.cerberu.cet.subtitle.domain.Subtitle;

import java.io.IOException;
import java.util.List;

/**
 * @author β世界
 * Created on 18:31
 * @Description Solr实现的搜索服务
 *
 * 存在问题：
 *      没有过滤参数，一次搜索全部，这并不合理
 *          处理方式：
 *              1.允许搜索所有，但是需要分页处理（推荐）
 *              2.不允许搜索所有，对solr的搜索关键字进行过滤
 */
public class SolrSearchServiceImpl implements SearchService {
    private static final Logger logger = LoggerFactory.getLogger(SolrSearchServiceImpl.class);

    private final Global global;
    private final SolrClient solrClient;

    public SolrSearchServiceImpl(Global global, SolrClient solrClient) {
        this.global = global;
        this.solrClient = solrClient;
    }

    @Override
    public List<Anime> searchAnime(String search, int pageNumber) {
        List<Anime> animes = null;
        try {
            SolrQuery solrQuery = new SolrQuery();
            // 设置搜索参数
            solrQuery.set("q","anime_name:"+search+" and alias:"+search);
            // 设置页码
            solrQuery.set("start", global.getPageShowNumber()*(pageNumber-1));
            solrQuery.set("rows", global.getPageShowNumber());
            QueryResponse response = solrClient.query("anime",solrQuery);
            animes = response.getBeans(Anime.class);
        } catch (SolrServerException | IOException e) {
            e.printStackTrace();
        }
        return animes;
    }

    @Override
    public List<Resource> searchResource(String search, int page) {
        List<Resource> resources = null;
        try {
            SolrQuery solrQuery = new SolrQuery();
            // 设置搜索参数
            solrQuery.set("q","resource_title:"+search);
            // 设置页码
            solrQuery.set("start", global.getPageShowNumber()*(page-1));
            solrQuery.set("rows",Integer.MAX_VALUE);
            QueryResponse response = solrClient.query("resource",solrQuery);
            List<Subtitle> subtitles = response.getBeans(Subtitle.class);
            resources = response.getBeans(Resource.class);
            // 进行映射处理
            if(subtitles.size() == resources.size()){
                for (int i = 0; i < resources.size(); i++) {
                    Resource resource = resources.get(i);
                    resource.setSubtitle(subtitles.get(i));
                    resource.setUpdate_time(resource.getUpdate_time().substring(0,resource.getUpdate_time().lastIndexOf(".")-1));
                }
            }
        } catch (SolrServerException | IOException e) {
            e.printStackTrace();
        }
        return resources;
    }

    /**
     * @author β世界 on 9:20 2021/3/19
     * 搜索字幕组
     * @param search: 搜索条件
     * @param page: 当前页码
     * @return java.util.List<xyz.cerberu.cet.subtitle.domain.Subtitle>
     **/
    @Override
    public List<Subtitle> searchSubtitle(String search, int page) {
        return null;
    }
}
